import { Component, OnInit, Input, OnDestroy } from '@angular/core';

import { ModalController } from '@ionic/angular';
import { AlertController } from '@ionic/angular';

import { Subscription } from 'rxjs';

import { MessageService } from 'src/app/shared/service/message.service';
import { HttpService } from 'src/app/shared/service/http.service';

import { AppGlobalService } from './../../shared/service/app-global.service';

@Component({
  selector: 'app-counselor-list',
  templateUrl: './counselor-list.component.html',
  styleUrls: ['./counselor-list.component.scss'],
})
export class CounselorListComponent implements OnInit, OnDestroy {

    // 组件数据
    @Input() tagData: any = {};
    // 声明订阅对象
    subscription: Subscription = null;

    // 筛选及页面数据参数
    params: any = {};

    // 当前页数，默认1
    pageNo = 1;

    // size
    pageSize: number;

    // 数量
    pageTotal: number;

    listData: any;

    defaultImg: String = '../../../assets/img/head.png';

    constructor(
        private appGlobalService: AppGlobalService,
        private message: MessageService,
        private http: HttpService,
        private modalController: ModalController,
        private alertController: AlertController
    ) {
    }

    ngOnInit() {

        console.log('ounselor-list data --->', this.tagData.data);
        // 初始化参数
        this.params = this.tagData.data.submit;
        this.params.sessionId = localStorage.getItem('sessionId');

        this.listData = this.tagData.data.data;

        this.pageSize = this.tagData.pageSize;
        this.pageTotal = this.tagData.pageTotal;

        if (!this.subscription) {
            // 获取筛选参数
            this.subscription = this.message.getMessage().subscribe(param => {
                // // 重置当前页数
                this.pageNo = 1;
                // 参数
                this.params.content = param;
                // 页面数据参数
                this.params.content.pageNo = this.pageNo;
                // 请求获取新数据
                this.http.post('application/submit', this.params, res => {
                    // 请求数据成功
                    if (res.success) {
                        const result = JSON.parse(res.data.data);
                        console.log(result);
                        // 筛选数据
                        this.listData = result.data;
                    } else {
                        // 请求数据失败
                        this.appGlobalService.showToast(res.message, 2000, 'middle');
                    }
                },
                err => {
                        // 请求数据失败
                        this.appGlobalService.showToast(err.message, 2000, 'middle');
                });
            });
        }
    }

    ngOnDestroy(): void {
        // 组件生命周期结束的时候，注销
        this.subscription.unsubscribe();
    }


    /**
     * 列表跳转
     * @param {*} obj 跳转对象
     * @memberof StoreListComponent
     */
    clickLink(obj) {
        if (!obj) {
            console.log('没有返回link-target');
        }
        const pageParams = {
            pageId: obj.target,
            params: obj.params,
            sessionId: localStorage.getItem('sessionId')
        };
        this.appGlobalService.goToForward(pageParams);
    }


    /**
     * TODO: 下拉刷新，保留
     *
     * @param {*} event
     * @memberof
     */
    doRefresh(event) {
    }

    /**
     * 上滑加载
     *
     * @param {*} event
     * @memberof
     */
    loadMoreData(event) {
        if (this.pageNo >= this.pageTotal) {
        //   this.appGlobalService.showToast('没有数据了...', 1000, 'middle');
          // event.complete();
          event.target.disable = true;
          return;
        }
        // // 请求下一个页面
        this.pageNo = this.pageNo + 1;
        // // 参数
        this.params.content.pageNo = this.pageNo;
        // 请求获取新数据
        this.http.post('application/submit', this.params, res => {

            // 请求数据成功
            if (res.success) {
              const resList = res.data.data;
                // 追加返回数据
              if (resList && resList.length > 0) {
                    // 追加数据
                    resList.map(item => {
                        this.listData.push(item);
                    });
                } else {
                    // 没有数据了
                    // this.appGlobalService.showToast('没有数据了...', 1000, 'middle');
                    event.target.disable = true;
                }
            } else {
                // 请求失败，更新当前页数
                this.pageNo = this.pageNo - 1;
                // 请求数据失败
                this.appGlobalService.showToast(res.message, 2000, 'middle');
            }
            event.target.complete();

        }, err => {
          // 请求失败，更新当前页数
          this.pageNo = this.pageNo - 1;
          // 请求数据失败
          this.appGlobalService.showToast(err.message, 2000, 'middle');
          event.target.disable = true;

        });

    }
    async call(mobile , name) {
        const alert = await this.alertController.create({
            header: this.tagData.data.nick_name,
            message: name + " " + mobile,
            buttons: [
                {
                    text: '取消',
                    role: 'cancel',
                    cssClass: 'secondary'
                }, {
                    text: '拨打',
                    handler: () => {
                        this.http.changeCallNumber(mobile);
                    }
                }
            ]
        });
        await alert.present();
    }
}
